package cn.edu.hust.sse.software7.class3.DAO;

import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.Transaction;

import cn.edu.hust.sse.software7.class3.common.PMF;
import cn.edu.hust.sse.software7.class3.entities.EBUser;

public class EBUserDAO {
	private static final Logger log = Logger.getLogger(EBUserDAO.class
			.getName());

	public boolean isExistUser(EBUser user) {
		log.info("check whether the user " + user.getUserName()
				+ " exist begin.");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		boolean result;
		try {
			tx.begin();
			Query query = pm.newQuery(EBUser.class);
			query.setFilter("userName == '" + user.getUserName() + "'");
			List<EBUser> users = (List<EBUser>) query.execute();
			if (users.isEmpty()) {
				log.finest("the user " + user.getUserName() + " isnot exist");
				result = false;
			} else {
				log.finest("the user  " + user.getUserName() + "already exist.");
				result = true;
			}
			tx.commit();
		} catch (Exception e) {
			log.info("check user exist occur exception!!!\n"
					+ e.getStackTrace());
			result = true;
		}
		return result;
	}

	public void addUser(EBUser user) {
		log.info("add user " + user.getUserName() + " begin.");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		try {
			tx.begin();
			pm.makePersistent(user);
			tx.commit();
			log.finest("add user " + user.getUserName() + " success.");
		} catch (Exception e) {
			log.info("add user " + user.getUserName() + "failed!!!\n"
					+ e.getStackTrace());
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
	}

	public void updateUser(EBUser user) {
		log.info("modify user " + user.getUserName() + " begin.");
		String userName = user.getUserName();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		try {
			tx.begin();
			EBUser user2Update = pm.getObjectById(EBUser.class,
					user.getUserId());
			user2Update.setBirth(user.getBirth());
			user2Update.setUserName(user.getUserName());
			user2Update.setEmail(user.getEmail());
			user2Update.setCellPhone(user.getCellPhone());
			user2Update.setPassword(user.getPassword());
			pm.makePersistent(user2Update);
			tx.commit();
			log.finest("modify user " + userName + " success.");
		} catch (Exception e) {
			log.info("modify user " + userName + "failed!!!\n"
					+ e.getStackTrace());
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
	}

	public EBUser validateUser(EBUser user) {
		log.info("validate User name exist begin.");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		EBUser u = null;
		try {
			tx.begin();
			Query query = pm.newQuery(EBUser.class);
			query.setFilter("userName == '" + user.getUserName() + "'"
					+" && "+ "password == '" + user.getPassword() + "'");
			List<EBUser> admins = (List<EBUser>) query.execute();
			if (admins.isEmpty()) {
				log.finest("validate User name " + user.getUserName()
						+ " failure");
			} else {
				log.finest("validate User name " + user.getUserName());
				u = admins.get(0);
			}
			tx.commit();
		} catch (Exception e) {
			log.info("validate User name occur exception!!!\n"
					+ e.getStackTrace());
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
		return u;
	}

	public List<EBUser> getAllUsers() {
		log.info("get All Users  begin.");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		List<EBUser> users = null;
		try {
			tx.begin();
			Query query = pm.newQuery(EBUser.class);
			users = (List<EBUser>) query.execute();
			tx.commit();
			log.finest("get All Users success.");
		} catch (Exception e) {
			log.info("get All Users occur exception!!!\n" + e.getStackTrace());
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
		return users;
	}

	public EBUser getUserById(Long userId) {
		log.info("get All Users  begin.");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		EBUser user = null;
		try {
			tx.begin();
			user = pm.getObjectById(EBUser.class, userId);
			tx.commit();
			log.finest("get All Users success.");
		} catch (Exception e) {
			log.info("get All Users occur exception!!!\n" + e.getStackTrace());
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
		return user;
	}

	public List<EBUser> getUsersByBirth(Date birth) {
		log.info("get Users By Birth  begin.");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		List<EBUser> users = null;
		try {
			tx.begin();
			Query query = pm.newQuery(EBUser.class);
			query.setFilter("birth == '" + birth + "'");
			users = (List<EBUser>) query.execute();
			tx.commit();
			log.finest("get Users By Birth success.");
		} catch (Exception e) {
			log.info("get Users By Birth occur exception!!!\n"
					+ e.getStackTrace());
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
		return users;
	}

	public List<EBUser> getUsersBirthOnToday() {
		log.info("get Users By Birth  begin.");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		List<EBUser> users = null;
		Date today = new Date();
		try {
			tx.begin();
			Query query = pm.newQuery(EBUser.class);
			query.setFilter("birth == '" + today + "'");
			users = (List<EBUser>) query.execute();
			tx.commit();
			log.finest("get Users By Birth success.");
		} catch (Exception e) {
			log.info("get Users By Birth occur exception!!!\n"
					+ e.getStackTrace());
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
		return users;
	}

	public List<EBUser> getUsersBirthOnWeek() {
		log.info("get Users By Birth  begin.");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		List<EBUser> users = null;
		try {
			tx.begin();
			Query query = pm.newQuery(EBUser.class);
			users = (List<EBUser>) query.execute();
			tx.commit();
			log.finest("get Users By Birth success.");
		} catch (Exception e) {
			log.info("get Users By Birth occur exception!!!\n"
					+ e.getStackTrace());
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
		return users;
	}

	public List<EBUser> getUsersByEmail(String email) {
		log.info("get Users By Email  begin.");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		List<EBUser> users = null;
		try {
			tx.begin();
			Query query = pm.newQuery(EBUser.class);
			query.setFilter("email == '" + email + "'");
			users = (List<EBUser>) query.execute();
			tx.commit();
			log.finest("get Users By Email success.");
		} catch (Exception e) {
			log.info("get Users By Email occur exception!!!\n"
					+ e.getStackTrace());
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
		return users;
	}

	public void deleteUserById(Long userId) {
		log.info("delete User  begin.");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		try {
			tx.begin();
			pm.deletePersistent(pm.getObjectById(EBUser.class, userId));
			tx.commit();
			log.finest("delete User success.");
		} catch (Exception e) {
			log.info("delete User occur exception!!!\n" + e.getStackTrace());
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
	}

	public List<EBUser> getAllUsers(Integer rangeValue) {
		log.info("get All Users  begin.");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		List<EBUser> users = null;
		try {
			tx.begin();
			Query query = pm.newQuery(EBUser.class);
			query.setRange(rangeValue * 10 - 10, rangeValue * 10);
			users = (List<EBUser>) query.execute();
			tx.commit();
			log.finest("get All Users success.");
		} catch (Exception e) {
			log.info("get All Users occur exception!!!\n" + e.getStackTrace());
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
		return users;
	}
}
